package com.pet.service.impl;

import com.pet.mapper.AdoptionApplicationMapper;
import com.pet.mapper.PetMapper;
import com.pet.pojo.AdoptionApplication;
import com.pet.pojo.PageRequest;
import com.pet.pojo.PageResult;
import com.pet.pojo.Pet;
import com.pet.service.AdoptionApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 领养申请服务层实现类
 */
@Service
public class AdoptionApplicationServiceImpl implements AdoptionApplicationService {
    
    @Autowired
    private AdoptionApplicationMapper applicationMapper;
    
    @Autowired
    private PetMapper petMapper;
    
    @Override
    @Transactional
    public boolean submitApplication(AdoptionApplication application) {
        // 设置默认状态为待审核
        if (application.getStatus() == null) {
            application.setStatus(0);
        }
        
        // 插入申请记录
        boolean result = applicationMapper.insertApplication(application) > 0;
        
        if (result) {
            // 更新宠物状态为领养中
            Pet pet = petMapper.selectPetById(application.getPetId());
            if (pet != null && pet.getStatus() == 0) { // 只有待领养的宠物才能更新状态
                pet.setStatus(2); // 领养中
                petMapper.updatePet(pet);
            }
        }
        
        return result;
    }
    
    @Override
    @Transactional
    public boolean updateApplicationStatus(Integer id, Integer status) {
        // 查询申请信息
        AdoptionApplication application = applicationMapper.selectApplicationById(id);
        if (application == null) {
            return false;
        }
        
        // 更新申请状态
        boolean result = applicationMapper.updateApplicationStatus(id, status) > 0;
        
        if (result) {
            // 如果申请通过，更新宠物状态为已领养
            if (status == 1) {
                Pet pet = petMapper.selectPetById(application.getPetId());
                if (pet != null) {
                    pet.setStatus(1); // 已领养
                    petMapper.updatePet(pet);
                }
            } 
            // 如果申请被拒绝，且没有其他待审核的申请，则将宠物状态改回待领养
            else if (status == 2) {
                // 查询该宠物是否还有其他待审核的申请
                PageRequest pageRequest = new PageRequest(1, 1);
                List<AdoptionApplication> applications = applicationMapper.selectApplicationsByPetId(application.getPetId(), pageRequest);
                boolean hasOtherApplications = applications.stream()
                        .anyMatch(app -> !app.getId().equals(id) && app.getStatus() == 0);
                
                if (!hasOtherApplications) {
                    Pet pet = petMapper.selectPetById(application.getPetId());
                    if (pet != null && pet.getStatus() == 2) { // 只有领养中的宠物才能改回待领养
                        pet.setStatus(0); // 待领养
                        petMapper.updatePet(pet);
                    }
                }
            }
        }
        
        return result;
    }
    
    @Override
    public AdoptionApplication getApplicationById(Integer id) {
        return applicationMapper.selectApplicationById(id);
    }
    
    @Override
    public PageResult<AdoptionApplication> getApplicationsByPetId(Integer petId, PageRequest pageRequest) {
        // 查询总记录数
        Long total = applicationMapper.selectApplicationsCountByPetId(petId);
        
        // 如果总记录数为0，直接返回空结果
        if (total == 0) {
            return new PageResult<>(0L, List.of(), 0, pageRequest.getPageNum(), pageRequest.getPageSize());
        }
        
        // 查询当前页数据
        List<AdoptionApplication> records = applicationMapper.selectApplicationsByPetId(petId, pageRequest);
        
        // 计算总页数
        int pages = (int) Math.ceil((double) total / pageRequest.getPageSize());
        
        // 返回分页结果
        return new PageResult<>(total, records, pages, pageRequest.getPageNum(), pageRequest.getPageSize());
    }
    
    @Override
    public PageResult<AdoptionApplication> getAllApplications(PageRequest pageRequest) {
        // 查询总记录数
        Long total = applicationMapper.selectApplicationsCount();
        
        // 如果总记录数为0，直接返回空结果
        if (total == 0) {
            return new PageResult<>(0L, List.of(), 0, pageRequest.getPageNum(), pageRequest.getPageSize());
        }
        
        // 查询当前页数据
        List<AdoptionApplication> records = applicationMapper.selectAllApplications(pageRequest);
        
        // 计算总页数
        int pages = (int) Math.ceil((double) total / pageRequest.getPageSize());
        
        // 返回分页结果
        return new PageResult<>(total, records, pages, pageRequest.getPageNum(), pageRequest.getPageSize());
    }
} 